package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.facade.CtrLoanContService;
import com.fdb.efp.nls.service.vo.CfcaSignInfoVO;
import com.fdb.efp.nls.service.dao.CfcaSignInfoDao;
import com.fdb.efp.nls.service.domain.CfcaSignInfo;
import com.fdb.efp.nls.service.facade.CfcaSignInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>ServiceImpl: CFCA加签信息				<p>
 * @author ligm
 * @date 2019-01-03
 */
@Service("cfcaSignInfoService")
public class CfcaSignInfoServiceImpl implements CfcaSignInfoService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(CfcaSignInfoServiceImpl.class);

	@Autowired
	private CfcaSignInfoDao cfcaSignInfoDao;
	
	@Autowired
	@Qualifier("ctrLoanContService")
	private CtrLoanContService ctrLoanContService;
	/**
	 * 新增操作
	 */
	@Override
	public int insertCfcaSignInfo(CfcaSignInfoVO inCfcaSignInfoVo) {
		logger.debug("当前新增数据为:"+ inCfcaSignInfoVo.toString());
		int num = 0;
		try {
			CfcaSignInfo cfcaSignInfo = new CfcaSignInfo();
			beanCopy(inCfcaSignInfoVo, cfcaSignInfo);
			num = cfcaSignInfoDao.insertCfcaSignInfo(cfcaSignInfo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(CfcaSignInfoVO inCfcaSignInfoVo) {
		logger.debug("当前删除数据条件为:"+ inCfcaSignInfoVo);
		int num = 0;
		try {
			CfcaSignInfo cfcaSignInfo = new CfcaSignInfo();
			beanCopy(inCfcaSignInfoVo, cfcaSignInfo);
			num = cfcaSignInfoDao.deleteByPk(cfcaSignInfo);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCfcaSignInfoVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(CfcaSignInfoVO inCfcaSignInfoVo) {
		logger.debug("当前修改数据为:"+ inCfcaSignInfoVo.toString());
		int num = 0;
		try {
			CfcaSignInfo cfcaSignInfo = new CfcaSignInfo();
			beanCopy(inCfcaSignInfoVo, cfcaSignInfo);
			num = cfcaSignInfoDao.updateByPk(cfcaSignInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCfcaSignInfoVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	@Override
	public CfcaSignInfoVO queryByPk(CfcaSignInfoVO inCfcaSignInfoVo) {
		
		logger.debug("当前查询参数信息为:"+ inCfcaSignInfoVo);
		try {
			CfcaSignInfo queryCfcaSignInfo = new CfcaSignInfo();
			beanCopy(inCfcaSignInfoVo, queryCfcaSignInfo);
			CfcaSignInfo queryRslCfcaSignInfo = cfcaSignInfoDao.queryByPk(queryCfcaSignInfo);
			if (Objects.nonNull(queryRslCfcaSignInfo)) {
				CfcaSignInfoVO outCfcaSignInfoVo = beanCopy(queryRslCfcaSignInfo, new CfcaSignInfoVO());
				logger.debug("当前查询结果为:"+ outCfcaSignInfoVo.toString());
				return outCfcaSignInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<CfcaSignInfoVO> queryAllOwner(CfcaSignInfoVO cfcaSignInfoVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<CfcaSignInfoVO> list = null;
		try {
			List<CfcaSignInfo> cfcaSignInfos = cfcaSignInfoDao.queryAllOwnerByPage(cfcaSignInfoVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ cfcaSignInfos.size());
			pageSet(cfcaSignInfos, cfcaSignInfoVo);
			list = (List<CfcaSignInfoVO>) beansCopy(cfcaSignInfos, CfcaSignInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<CfcaSignInfoVO> queryAllCurrOrg(CfcaSignInfoVO cfcaSignInfoVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<CfcaSignInfo> cfcaSignInfos = cfcaSignInfoDao.queryAllCurrOrgByPage(cfcaSignInfoVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+cfcaSignInfos.size());
		List<CfcaSignInfoVO> list = null;
		try {
			pageSet(cfcaSignInfos, cfcaSignInfoVo);
			list = (List<CfcaSignInfoVO>) beansCopy(cfcaSignInfos, CfcaSignInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<CfcaSignInfoVO> queryAllCurrDownOrg(CfcaSignInfoVO cfcaSignInfoVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<CfcaSignInfo> cfcaSignInfos = cfcaSignInfoDao.queryAllCurrDownOrgByPage(cfcaSignInfoVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ cfcaSignInfos.size());
		List<CfcaSignInfoVO> list = null;
		try {
			pageSet(cfcaSignInfos, cfcaSignInfoVo);
			list = (List<CfcaSignInfoVO>) beansCopy(cfcaSignInfos, CfcaSignInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CfcaSignInfoVO> queryCfcaSignInfoConditionByPage(CfcaSignInfoVO cfcaSignInfoVo, String dataRuleCode) {
		logger.debug("当前查询数据信息的参数信息为:" + cfcaSignInfoVo + "======>数据权限为:" + dataRuleCode);
		List<CfcaSignInfoVO> list = null;
		try {
			//List<CfcaSignInfo> cfcaSignInfos = cfcaSignInfoDao.queryAllOwnerByPage(cfcaSignInfoVo);
			List<CfcaSignInfo> cfcaSignInfos = this.dataRuleFilterByPage(cfcaSignInfoVo, dataRuleCode);
			logger.debug("当前查询数据信息的结果集数量为:"+ cfcaSignInfos.size());
			pageSet(cfcaSignInfos, cfcaSignInfoVo);
			list = (List<CfcaSignInfoVO>) beansCopy(cfcaSignInfos, CfcaSignInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	/**
	 * 
	 * 需求编号：【】（业务提的需求编号）
	 * 问题编号：【3313/3312】（若不是因为修复问题而新增该类，则留空）
	 * 开发人员：【wanglh4】
	 * 创建日期：【2019年7月25日】
	 * 功能描述：前台页面数据展示
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<CfcaSignInfoVO> queryCfcaInfoSByPage(CfcaSignInfoVO cfcaSignInfoVo, String dataRuleCode) {
		logger.debug("当前查询数据信息的参数信息为:" + cfcaSignInfoVo + "======>数据权限为:" + dataRuleCode );
		List<CfcaSignInfoVO> list = null;
		try {
			//List<CfcaSignInfo> queryCfcaSignInfoS = cfcaSignInfoDao.queryCfcaInfoByPage(cfcaSignInfoVo);
			List<CfcaSignInfo> queryCfcaSignInfoS = this.dataRuleFilterByPage(cfcaSignInfoVo, dataRuleCode);
			pageSet(queryCfcaSignInfoS, cfcaSignInfoVo);
			list = (List<CfcaSignInfoVO>) beansCopy(queryCfcaSignInfoS, CfcaSignInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}
	
	/**
	 * 
	 * 需求编号：【】（业务提的需求编号）
	 * 问题编号：【3313/3312】（若不是因为修复问题而新增该类，则留空）
	 * 开发人员：【wanglh4】
	 * 创建日期：【2019年7月25日】
	 * 功能描述：导出数据查询（查询条件证件类型，证件号码，合同类型，归属机构为空）
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<CfcaSignInfoVO> queryCfcaSignInfoCondition(CfcaSignInfoVO cfcaSignInfoVo, String dataRuleCode) {
		logger.debug("当前查询导出数据信息的参数信息为:" + cfcaSignInfoVo + "======>数据权限为:" + dataRuleCode);
		List<CfcaSignInfoVO> list = null;
		try {
			//List<CfcaSignInfo> cfcaSignInfos = cfcaSignInfoDao.queryAllOwner(cfcaSignInfoVo);
			List<CfcaSignInfo> cfcaSignInfos = this.dataRuleFilter(cfcaSignInfoVo, dataRuleCode);
			logger.debug("当前查询导出数据信息的结果集数量为:"+ cfcaSignInfos.size());
			list = (List<CfcaSignInfoVO>) beansCopy(cfcaSignInfos, CfcaSignInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	/**
	 * 
	 * 需求编号：【】（业务提的需求编号）
	 * 问题编号：【3313/3312】（若不是因为修复问题而新增该类，则留空）
	 * 开发人员：【wanglh4】
	 * 创建日期：【2019年7月25日】
	 * 功能描述：导出数据查询（查询条件证件类型，证件号码，合同类型，归属机构不为空）
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<CfcaSignInfoVO> queryCfcaInfo(CfcaSignInfoVO cfcaSignInfoVo, String dataRuleCode) {
		logger.debug("当前查询导出数据信息的参数信息为:" + cfcaSignInfoVo + "======>数据权限为:" + dataRuleCode);
		List<CfcaSignInfoVO> list = null;
		try {
			//List<CfcaSignInfo> queryCfcaSignInfoS = cfcaSignInfoDao.queryCfcaInfo(cfcaSignInfoVo);
			List<CfcaSignInfo> queryCfcaSignInfoS = this.dataRuleFilter(cfcaSignInfoVo, dataRuleCode);
			pageSet(queryCfcaSignInfoS, cfcaSignInfoVo);
			list = (List<CfcaSignInfoVO>) beansCopy(queryCfcaSignInfoS, CfcaSignInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	private List<CfcaSignInfo> dataRuleFilterByPage(CfcaSignInfoVO cfcaSignInfoVo, String dataRuleType){
		List<CfcaSignInfo> result = null;
		switch (dataRuleType) {
			case "01":
				result = cfcaSignInfoDao.queryAllOwnerByPage(cfcaSignInfoVo);
				break;
			case "02":
				result = cfcaSignInfoDao.queryAllCurrOrgByPage(cfcaSignInfoVo);
				break;
			case "03":
				result = cfcaSignInfoDao.queryAllCurrDownOrgByPage(cfcaSignInfoVo);
				break;
			case "04":
				result = cfcaSignInfoDao.queryAllCurrOwnerPrdByPage(cfcaSignInfoVo);
				break;
			default:
				result = cfcaSignInfoDao.queryAllCurrOrgByPage(cfcaSignInfoVo);
				break;
		}
		return result;
	}

	private List<CfcaSignInfo> dataRuleFilter(CfcaSignInfoVO cfcaSignInfoVo, String dataRuleType){
		List<CfcaSignInfo> result = null;
		switch (dataRuleType) {
			case "01":
				result = cfcaSignInfoDao.queryAllOwner(cfcaSignInfoVo);
				break;
			case "02":
				result = cfcaSignInfoDao.queryAllCurrOrg(cfcaSignInfoVo);
				break;
			case "03":
				result = cfcaSignInfoDao.queryAllCurrDownOrg(cfcaSignInfoVo);
				break;
			case "04":
				result = cfcaSignInfoDao.queryAllCurrOwnerPrd(cfcaSignInfoVo);
				break;
			default:
				result = cfcaSignInfoDao.queryAllCurrOrg(cfcaSignInfoVo);
				break;
		}
		return result;
	}

	@Override
	public List<CfcaSignInfoVO> queryByApplyAndContNo(CfcaSignInfoVO cfcaSignInfoVO) throws Exception {
		List<CfcaSignInfoVO> cfcaSignInfoVOS = (List<CfcaSignInfoVO>) beansCopy(cfcaSignInfoDao.queryByApplyAndContNo(beanCopy(cfcaSignInfoVO, new CfcaSignInfo())), CfcaSignInfoVO.class);
		return cfcaSignInfoVOS;
	}


}
